--RemoteLogUI.lua
require 'fairygui.Window'


local fexists    = stdfs.exists

RemoteLogUI = class(Window)
local prop = property(RemoteLogUI)
prop:accessor("treeView", nil)
prop:accessor("defaultNode", nil)

function RemoteLogUI:__init()
    print("[RemoteLogUI][__init]")
    self:loadLayout("RemoteLogUI", "Main")
end

-- 初始化(仅执行一次)
function RemoteLogUI:initEvent()
    self.treeView = self:getChild("treeView")
    -- 树渲染
    self.treeView.treeNodeRender = function(node, obj)
        obj.text = node.data.name
        self:upNodeController(node)
        if not node.isFolder and not node.data.click then
            node.data.click = true
            node.cell:GetChild("btn_open").onClick:Add(function()
                local cmd = 'explorer /select,"' .. node.data.client_path:gsub("/", "\\") .. '"'
                print("cmd:"..cmd)
                io.popen(cmd, 'r')
            end)
            node.cell:GetChild("btn_download").onClick:Add(function()
                g_log_mgr:downLogFile(node.data.dirs,node.data.server_path, node.data.client_path, function (err)
                    if err then
                        g_event_mgr:notifyListener("onWarnTip", "下载失败")
                        print("[RemoteLogUI][initEvent] downLogFile fail server_path:"..node.data.server_path)
                        return
                    end
                    self:upNodeController(node)
                    g_event_mgr:notifyListener("onSuccessTip", "下载成功")
                end)
            end)
            node.cell:GetChild("btn_refresh").onClick:Add(function()
                g_log_mgr:downLogFile(node.data.dirs,node.data.server_path, node.data.client_path, function (err)
                    if err then
                        g_event_mgr:notifyListener("onWarnTip", "刷新失败")
                        print("[RemoteLogUI][initEvent] downLogFile fail server_path:"..node.data.server_path)
                        return
                    end
                    self:upNodeController(node)
                    g_event_mgr:notifyListener("onSuccessTip", "刷新成功")
                end)
            end)
        end
    end
    -- 树下拉刷新
    self.treeView.scrollPane.onPullDownRelease:Add(function()
        local header = self.treeView.scrollPane.header;
        if header:GetController("c1").selectedIndex == 1 then
            header:GetController("c1").selectedIndex = 2;
            self.treeView.scrollPane:LockHeader(header.sourceHeight);
            local callback = function(err)
                g_time_mgr:add(1, 1, function()
                if err then
                    g_event_mgr:notifyListener("onWarnTip", err)
                    header:GetController("c1").selectedIndex = 0;
                    self.treeView.scrollPane:LockHeader(0);
                    return
                end
                header:GetController("c1").selectedIndex = 3;
                self.treeView.scrollPane:LockHeader(35);
                g_time_mgr:add(2, 1, function()
                    header:GetController("c1").selectedIndex = 0;
                    self.treeView.scrollPane:LockHeader(0);
                end)
            end)
            end
            self:load(callback)
        end
    end)
    self.treeView.scrollPane.header.onSizeChanged:Add(function()
        local selectedIndex = self.treeView.scrollPane.header:GetController("c1").selectedIndex;
        if selectedIndex == 2 or selectedIndex == 3 then
            return;
        end
        local c1 = self.treeView.scrollPane.header:GetController("c1");
        if self.treeView.scrollPane.header.height > self.treeView.scrollPane.header.sourceHeight then
            c1.selectedIndex = 1;
        else
            c1.selectedIndex = 0;
        end
    end);
    self:getChild("close").onClick:Add(function()
        self:close()
    end)
end

function RemoteLogUI:upNodeController(node)
    if fexists(node.data.client_path) then
        node.cell:GetController("fexists").selectedIndex = 0
    else
        node.cell:GetController("fexists").selectedIndex = 1
    end
end

function RemoteLogUI:initTreeUI(node, parentNode)
    -- 叶子节点
    if node.is_file then
        local new_node = GTreeNode(false)
        new_node.data = {
           name = node.name,
           dirs = node.dirs,
           server_path = node.server_path,
           client_path = node.client_path,
           time = node.datetime,
           click = false,
        }
        if parentNode then
            parentNode:AddChild(new_node)
        else
            self.treeView.rootNode:AddChild(new_node)
        end
        if not self.defaultNode then
            self.defaultNode = new_node
        end
        return new_node
    end
    -- 父节点
    local new_node = GTreeNode(true)
    new_node.data = {
        name = node.name
    }
    if parentNode then
        parentNode:AddChild(new_node)
    else
        self.treeView.rootNode:AddChild(new_node)
    end
    -- 遍历子节点
    for _, child in ipairs(node.children) do
        self:initTreeUI(child, new_node)
    end
    return new_node
end

function RemoteLogUI:load(callback)
    g_log_mgr:load(function(err)
        if err then
            g_event_mgr:notifyListener("onWarnTip", err)
            return
        end
        self.defaultNode = nil
        local node = self:initTreeUI(g_log_mgr.log_tree)
        if self.defaultNode then
            self.treeView:SelectNode(self.defaultNode)
        end
        if callback then
            callback()
        end
    end)
end

-- 打开UI执行一次
function RemoteLogUI:initComponent()
    self.treeView:RemoveChildren()
    self:load()
end

function RemoteLogUI:onClose()
end